using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;

namespace Pokemon
{
    class Pikachu : Pokemon
    {

        #region Attributes

        private static Card _avatar;

        //private static List<Texture2D> _textures = new List<Texture2D>();

        //private static List<Vector2> _frameSize = new List<Vector2>();

        //private static List<Vector2> _centerPoint = new List<Vector2>();

        //private static int _delay;//Cho biet thoi gian delay

        //private static int _range;//Thuoc tinh dung de tinh khoang cach co the ra skill cua 1 unit

        //private static int _health;

        //private static int _damage;

        //private static int _bulledSpeed;

        //private static Game _game; //Thuoc tinh de luu bien game, dung cho ham Initialize

#endregion

        #region Properties

        public static Card Avatar
        {
            get { return _avatar; }
            set { _avatar = value; }
        }

#endregion

        public Pikachu(Game game, Blank owner, Vector2 position)
            : base(game, owner, position)
        {           
            
        }

        public override void Initialize()
        {
            XDocument doc = XDocument.Load(@"Content/Textures/Pokemons/Pikachu/PikachuDef.xml");
            Texture2D texture2;
            Vector2 frameSize = Vector2.Zero;
            Vector2 centerPoint = Vector2.Zero;
            int numFrame;

            XName name = XName.Get(@"Damages");
            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                _damage = Int32.Parse((definitions.First()).Value);

                name = XName.Get("Healths");
                definitions = doc.Document.Descendants(name);
                _health = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get("Speed");
                definitions = doc.Document.Descendants(name);
                _bulledSpeed = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get("Range");
                definitions = doc.Document.Descendants(name);
                _range = Int32.Parse((definitions.First()).Value);

                name = XName.Get("Delay");
                definitions = doc.Document.Descendants(name);
                _delay = Int32.Parse((definitions.First()).Value);                

                name = XName.Get(@"Definition");
                definitions = doc.Document.Descendants(name);
                
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        texture2 = Game.Content.Load<Texture2D>(xAttribute.Value);
                        
                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = float.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = float.Parse(xAttribute.Value);
                                frameSize = new Vector2(width, height);                                
                            }
                        }

                        xAttribute = definition.Attribute(@"XCenterPoint");
                        var xCenterPoint = Int32.Parse(xAttribute.Value);
                        
                        xAttribute = definition.Attribute(@"YCenterPoint");
                        var yCenterPoint = Int32.Parse(xAttribute.Value);
                        centerPoint = new Vector2(xCenterPoint, yCenterPoint);

                        xAttribute = definition.Attribute(@"NumFrame");
                        numFrame = Int32.Parse(xAttribute.Value);

                        xAttribute = definition.Attribute(@"Name");
                        var type = xAttribute.Value;

                        Sprite sprite = new Sprite(texture2, numFrame, frameSize, centerPoint, false);

                        switch (type)
                        {
                            case "Idle":
                                Textures[0] = sprite;
                                break;
                            case "Attack":
                                Textures[1] = sprite;
                                break;
                            case "Hit":
                                Textures[2] = sprite;
                                break;
                            case "Dead":
                                Textures[3] = sprite;
                                break;
                            case "Stand":
                                Textures[4] = sprite;
                                break;
                        }                        
                    }
                }
            }      
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _TimeSpan += gameTime.ElapsedGameTime;
            if (UseSkill != null)
            {
                OnUseSkill();
                return;
            }
                
            _delayTimeSpan += gameTime.ElapsedGameTime;                        
            if (_TimeSpan.Milliseconds < 300) //300mil update
                return;
            base.Update(gameTime);
            _TimeSpan = TimeSpan.Zero;    
        
            if (_enemy != null)
            {
                if (Blank.GetDistance(GetCenterPoint(), _enemy.GetCenterPoint()) < _range)
                {
                    if (_UnitState == UnitState.Idle)//Neu dang o trang thai binh thuong thi chuyen sang frame trang thai attack
                        Attack();
                    else
                    //    if (_frameIndex == 6)
                    //        _enemy.TakeDamage(Damage);                    
                        if (_delayTimeSpan.Seconds >= _delay)   //Khoang time giua 2 lan ra chieu dax vuot qua delayTime 
                                                                //-> Cho phep tao 1 dot tan cong moi
                        {
                            Shot(_enemy);
                            _delayTimeSpan = TimeSpan.Zero;
                        }                        
                }
                else
                    if (_UnitState == UnitState.Attack)
                        Idle();
            }
            else
            {
                if (_UnitState == UnitState.Attack)
                    Idle();
            }
            Textures[GetUnitStateIndex()].Update();            
        }

        public override void Draw(GameTime gameTime)
        {            
            Rectangle rect = GetNextRect();
            SpriteBatch _SpriteBatch = (SpriteBatch)Game.Services.GetService(typeof (SpriteBatch));
            Textures[GetUnitStateIndex()].Draw(_SpriteBatch, Position, rect, Color.White, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0);  
            base.Draw(gameTime);
                                                                                                      
        }

        public override void Shot(Unit target)
        {
            Lightning bullet = new Lightning(Game, this, _enemy);
            AddEvent();
            BulletManager b = (BulletManager)Game.Services.GetService(typeof(BulletManager));
            b.Add(bullet);
        }

        #region Event: Xu ly viec dong bo hoa sprite luc dung skill

        public delegate void UsingSkill();

        private event UsingSkill UseSkill;

        public void OnUseSkill()
        {
            UsingSkill handler = UseSkill;
            if (handler != null) handler();
        }

        private void PikachuOnSkill()
        {
            Textures[GetUnitStateIndex()].FrameIndex = 4;
        }
        
        public void AddEvent()//gan event vao de doi tuong biet la no dang ra skill
        {
            UseSkill += PikachuOnSkill;
        }

        public void RemoveEvent()
        {
            UseSkill -= PikachuOnSkill;
        }

        public override Point GetCenterPoint()
        {
            var index = GetUnitStateIndex();
            return
                new Point((int)_position.X + (int)Textures[index].CenterPoint.X,
                    (int)_position.Y + (int)Textures[index].CenterPoint.Y);
        }

        #endregion
    }
}
